package com.easy.cms.material.controller;

import com.easy.cms.base.CommonResult;
import com.easy.cms.material.model.Material;
import com.easy.cms.material.model.MaterialQuery;
import com.easy.cms.material.service.MaterialService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;

@RestController
@RequestMapping("/api/material")
public class MaterialController {
    @Resource
    private MaterialService materialService;
    @GetMapping("/list")
    public CommonResult<Iterator<Material>> getMaterialList() {
        return materialService.getMaterialList();
    }
    @GetMapping("/search")
    public CommonResult<java.util.List<Material>> searchMaterials(@RequestParam(required = false) String code, @RequestParam(required = false) String name) {
        return materialService.searchMaterials(code, name);
    }

    @PostMapping("/add")
    public CommonResult<Material> addMaterial(@Valid @RequestBody Material material) {
        return materialService.addMaterial(material);
    }
    @PutMapping("/update")
    public CommonResult<Material> updateMaterial(@RequestBody Material material) {
        return materialService.updateMaterial(material);
    }
    @GetMapping("/listPage")
    public CommonResult<Page<Material>> getMaterialListPage(@RequestBody MaterialQuery materialQueryWithPagination) {
        return materialService.getMaterialListPage(materialQueryWithPagination);
    }
    @DeleteMapping("/delete/{id}")
    public CommonResult<?> deleteMaterial(@PathVariable Long id) {
        return materialService.deleteMaterial(id);
    }
}